<template>
  <el-form ref="form" :model="detail"  :rules="rules" label-width="80px">
    <el-form-item label="真实姓名" prop="realName"  >
      <el-input v-model="detail.realName" maxlength="30"  />
    </el-form-item>
    <el-form-item label="身份证号" prop="identityCard">
      <el-input v-model="detail.identityCard"  />
    </el-form-item>

    <el-form-item label="学历信息" prop="educationLevel">
      <el-select v-model="detail.educationLevel" >
        <el-option v-for="dict in dictsEducationLevel" :key="dict.dictValue" :label="dict.dictLabel"
          :value="dict.dictLabel" />
      </el-select>
    </el-form-item>



    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>
  
<script>
import {getUserDetail,updateUserDetailProfile} from "@/api/credit/creUser";


export default {
  name: "",
  components: {  getUserDetail,updateUserDetailProfile },
  props: {
    user: {
      type: Object
    },
    percentage:{
      type: Number
    }
  },
  data() {
    return {
      detail: [],
      dictsEducationLevel: '',
      // userid: 0,
      // 表单校验
      rules: {
        realName: [
          { required: true, message: "真实姓名不能为空", trigger: "blur" },
          {
            pattern: /^[\u4e00-\u9fa5]{0,}$/,
            message: "姓名不符合规范",
            trigger: "blur"
          }
        ],
        identityCard: [
          { required: true, message: "身份证信息不能为空", trigger: "blur" },
          {
            pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
            message: "身份证信息不符合规范",
            trigger: "blur"
          }
        ],
        educationLevel: [
          { required: true, message: "学历信息不能为空", trigger: "blur" },
        ]
      }
    };
  },
  created() {
    this.getDicts('cre_education_level').then(res => {
      this.dictsEducationLevel = res.data
    })

  },
  methods: {
    findUserDetail() {
      getUserDetail(this.user.userId).then(res => {
        this.detail = res.data
        // this.percentageInitialization()
      }).catch()
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserDetailProfile(this.detail).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.increase()
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
    increase() {
        if (this.percentage > 100) {
          this.percentage = 100;
        }
        console.log(this.percentage);
      this.$emit('increase',this.percentage);
      },
      decrease() {
        this.percentage -= 10;
        if (this.percentage < 0) {
          this.percentage = 0;
        }
      },
      percentageInitialization() {
      this.percentage = 0
      if (this.user.phonenumber) {
        this.percentage += 5
      }
      if (this.user.email) {
        this.percentage += 5
      }
      if (this.user.sex != "2") {
        this.percentage += 5
      }
      if (this.detail.realName) {
        this.percentage += 5
      }
      if (this.detail.identityCard) {
        this.percentage += 5
      }
      if (this.detail.educationLevel) {
        this.percentage += 5
      }
      if (this.detail.bankCard) {
        this.percentage += 5
      }
      if (this.detail.occupation) {
        this.percentage += 5
      }
      if (this.detail.monthIncome) {
        this.percentage += 5
      }
      if (this.detail.address) {
        this.percentage += 5
      }
      if (this.detail.houseCertificate) {
        this.percentage += 5
      }
      if (this.detail.emergencyPhone) {
        this.percentage += 5
      }
      if (this.detail.password) {
        this.percentage += 5
      }
      if (this.user.userName) {
        this.percentage += 5
      }
    }
  },
  watch: {
    user: function (newData, oldData) {
      console.log(oldData, newData);
      this.findUserDetail()
    },
    percentage:function(newData,oldData){
      console.log(oldData, newData);
      this.percentage=newData
    }
  }
};
</script>
  